package com.hmkj.quote.job;

import com.hmkj.core.constant.CacheID;
import com.hmkj.core.mapper.order.OrderSimulateMapper;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.order.OrderSimulate;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.order.OrderSimulateService;
import com.hmkj.core.service.risk.RiskParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟订单处理定时器
 *
 * @author eproo
 * @create 2017-09-20 14:39
 */
@Slf4j
@Component
public class OrderSimulateJob {

    @Resource
    private OrderSimulateMapper orderSimulateMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private RiskParamService riskParamService;
    @Resource
    private OrderSimulateService orderSimulateService;

    /**
     * 平仓定时查询
     */
    @Scheduled(cron="0 * * * * ?")
    public void coverOrderSimulateTiming() {
        //获取点买单的订单列表
        List<Commo> commoList = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        for (Commo commo : commoList) {
            // 判断当前时间该品种是否需要进行 日内强平
            if (riskParamService.isAutoCoverTime(commo.getId())) {
                Map<String,Object> params = new HashMap<>();
                params.put("type", User.TYPE.T3.code);
                params.put("status", OrderSimulate.STATUS.T3.code);
                params.put("commoId", commo.getId());
                params.put("delFlag",OrderSimulate.DELFLAG.NORMAL.code);
                List<OrderSimulate> list = orderSimulateMapper.findNotTradeOrderSimulateList(params);
                for (OrderSimulate os : list) {
                    try {
                        orderSimulateService.coverOrderSimulate(os,os.getNumber(),OrderSimulate.SETTLETYPE.T4.code);
                    } catch (Exception e) {
                        log.error("模拟订单平仓失败，订单Id:" + os.getId());
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    /**
     * 自动撤单定时器
     */
    @Scheduled(cron = "0 * * * * ?")//每分钟钟查询一次
    public void cancleOrderTiming() {
        //获取点买单的订单列表
        List<Commo> commoList = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        commoList.forEach(commo -> {
            // 判断当前时间该品种是否需要进行 日内强平
            if (riskParamService.isAutoCoverTime(commo.getId())) {
                Map<String,Object> params = new HashMap<>();
                params.put("type", User.TYPE.T3.code);
                params.put("status", OrderSimulate.STATUS.T2.code);
                params.put("commoId", commo.getId());
                params.put("delFlag",OrderSimulate.DELFLAG.NORMAL.code);
                List<OrderSimulate> list = orderSimulateMapper.findNotTradeOrderSimulateList(params);
                list.forEach(os -> {
                    try {
                        orderSimulateService.cancelOrder(os);
                    } catch (Exception e) {
                        log.error("模拟订单撤单失败，订单Id:" + os.getId());
                        e.printStackTrace();
                    }
                });
            }
        });
    }

    /**
     * 流单定时查询
     */
    @Scheduled(cron="0/1 * * * * ?")// 每一秒钟查询一次
    public void flowOrderTiming() {
        //获取待流单的订单列表
        Map<String, Object> params = new HashMap<>();
        params.put("status", OrderSimulate.STATUS.T1.code);
        List<OrderSimulate> orderSimulates = orderSimulateMapper.selectWaitFlowOrderSimulate(params);
        log.info("已查询到待流单模拟订单数："+orderSimulates.size()+"条");
        //放入待流单队列
        orderSimulates.forEach(orderSimulate -> {
            redisService.push(CacheID.QUEUE_FLOW_ORDER_SL,orderSimulate);
        });
    }
}
